package com.cn.test;

import com.cn.mapper.UserMapper1;
import com.cn.pojo.User;
import com.cn.utils.SqlSessionUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 3.2 参数传递与接收
 */
public class UserTest1 {
    /**
     * 3.2.1 传递和接收单个普通类型参数
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        int id = 1;
        User user = mapper.queryUserById(id);
        System.out.println(user);
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.2 传递和接收多个普通类型参数
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        List<User> users = mapper.queryUsersByUsernameAndGender("lucy", "female");
        System.out.println(users);
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.3 利用@Param注解传递多个普通类型参数
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        List<User> users = mapper.queryUsersByUsernameOrGender("lucy", "女");
        for (User user : users) {
            System.out.println(user);
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.4 传递和接收单个POJO参数，映射文件使用#{POJO属性名}
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        User user = new User(null, "佳佳", "123456", "female");
        int i = mapper.insertUser(user);
        if(i>0){
            System.out.println("插入成功");
        }else{
            System.out.println("插入失败");
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.5 传递和接收多个POJO参数
     * 当传递的参数是多个POJO参数时可使用#{paramN.POJO的属性名}的形式获取对应的参数
     * @throws IOException
     */
    @Test
    public void test5() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        //创建第一个User对象
        User user1 = new User(null, "佳佳", "123456", "female");
        //创建第二个User对象
        User user2 = new User(null, "lucy", "123456", "female");
        List<User> users = mapper.queryUsersByTwoUser(user1, user2);
        for (User user : users) {
            System.out.println(user);
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }


    /**
     * 3.2.6 利用@Param注解传递多个POJO类型参数
     * @throws IOException
     */
    @Test
    public void test6() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        //创建第一个User对象
        User user1 = new User(null, "佳佳", "123456", "female");
        //创建第二个User对象
        User user2 = new User(null, "lucy", "123456", "female");
        List<User> users = mapper.queryUsersByTwoUserWithParam(user1,user2);
        for (User user : users) {
            System.out.println(user);
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.7 传递和接收Map类型参数
     * @throws IOException
     */
    @Test
    public void test7() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        Map<String ,Object> paramMap = new HashMap<>();
        paramMap.put("username","lucy");
        paramMap.put("gender","female");
        List<User> users = mapper.queryUsersByMap(paramMap);
        for (User user : users) {
            System.out.println(user);
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.8 传递和接收List类型参数
     */
    @Test
    public void test8() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(2);
        List<User> users = mapper.queryUsersByList(idList);
        for (User user : users) {
            System.out.println(user);
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }

    /**
     * 3.2.9 传递和接收数组类型参数
     * @throws IOException
     */
    @Test
    public void test9() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
        int[] idList = {1,2,3};
        List<User> users = mapper.queryUsersByArray(idList);
        for (User user : users) {
            System.out.println(user);
        }
        SqlSessionUtil.closeSqlSession(sqlSession);
    }


}
